#include <vector>
#include <set>
#include <iostream>
#include <unordered_map>
#include <numeric>

using namespace std;

vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
    int xlen = mat.size(), ylen = mat[0].size();
    if(r * c != xlen * ylen || xlen * ylen % r != 0){
        return mat;
    }
    vector<vector<int>> ret(r, vector<int> (c, 0));
    int x = 0, y = -1;
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            y < ylen-1 ? ret[i][j] = mat[x][++y] : ret[i][j] = mat[++x][y = 0];
        }
    }
    return ret;
}

int main(){
    vector<vector<int>> mat = { {1,2},{3,4} };
    vector<vector<int>> ret = matrixReshape(mat, 2, 4);
    // for(int i = 0; i < ret.size(); i++){
    //     cout << ret[i] << endl;
    // }
    return 0;
}